<template>
  <div id="container">
    <div class="navbar">
      <img :src="logo" alt class="float-left" height="60px"/>
    </div>
    <div class="info">
      <div class="info-img">
        <img :src="docPic" alt height="120px"/>
        <p style="font-size:18px">密码找回</p>
      </div>
      <el-form ref="pwdform" class="info-form" :rules="rules" :model="pwdform" label-width="200px">
        <el-form-item label="新密码" prop="pass">
          <el-input
            type="password"
            v-model="pwdform.pass"
            placeholder="请输入新密码（6-18位），可包含数字、大/小写字母、特殊字符"
            maxlength="18"
            show-password

          >
          </el-input>
          <div class="pwd_level">
            <div class="level_label">密码安全等级：</div>
            <div class="level_box">
              <div class="level_item" :class="level>=1?'level_item_1':''"></div>
              <div class="level_item" :class="level>=2?'level_item_2':''"></div>
              <div class="level_item" :class="level>=3?'level_item_3':''"></div>
              <div class="level_item" :class="level>=4?'level_item_4':''"></div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="确认新密码" prop="checkPass">
          <el-input
            type="password"
            v-model="pwdform.checkPass"
            placeholder="再次确认新密码"
            maxlength="18"
            show-password
          >
          </el-input>
        </el-form-item>
      </el-form>

      <div class="info-bottom">
        <el-button type="primary" class="info-btn" @click="submitForm('pwdform')">提交</el-button>
        <el-button type="default" class="info-btn" @click="goBack">返回</el-button>
      </div>

      <!-- <div slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        :loading="loading"
        @click="submitForm('pwdform')"
        style="width:100px"
      >保存</el-button>
    </div> -->

    </div>
  </div>
</template>

<script>
import { putSysUserChangePasswordByEncrypt } from '@/api/api.js'
import { hex_md5 } from '@/utils/md5.js'
import Logo from '@/assets/images/logo-light.png'
import DocPic from '@/assets/images/doctor_lg.png'

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        if (value.length < 6) {
          callback(new Error('密码不能低于6位数'))
        } else if (this.pwdform.checkPass !== '') {
          this.$refs.pwdform.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.pwdform.pass) {
        callback(new Error('两次输入新密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      logo: Logo,
      docPic: DocPic,
      loading: false,
      // visible1: false,
      // visible2: false,
      // visible3: false,
      pwdform: {
        password: '',
        pass: '',
        checkPass: ''
      },
      rules: {
        password: [
          { required: true, message: '请输入当前密码', trigger: 'blur' }
        ],
        pass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' }
        ]
      },
      level: 0
    }
  },
  watch: {
    'pwdform.pass': function (val, oldVal) {
      this.level = this.AnalyzePasswordSecurityLevel(val)
    }
  },
  methods: {
    submitForm(formName) {
      const that = this

      this.$refs[formName].validate((valid) => {
        if (valid) {
          that.loading = true
          const params = {
            oldPassword: hex_md5(that.pwdform.password).toUpperCase(),
            newPassword: hex_md5(that.pwdform.pass).toUpperCase(),
            confirmPassword: hex_md5(that.pwdform.checkPass).toUpperCase()
          }
          putSysUserChangePasswordByEncrypt(params).then((res) => {
            if (res && res.code === 10000) {
              that.$message.success('密码修改成功！')
              that.pwdform.password = ''
              that.pwdform.pass = ''
              that.pwdform.checkPass = ''
            }
            that.loading = false
          })
        } else {
          return false
        }
      })
    },
    AnalyzePasswordSecurityLevel(password) {
      let securityLevelFlag = 0
      if (password.length < 6) {
        return 0
      } else {
        if (/[a-z]/.test(password)) {
          securityLevelFlag++ // lowercase
        }
        if (/[A-Z]/.test(password)) {
          securityLevelFlag++ // uppercase
        }
        if (/[0-9]/.test(password)) {
          securityLevelFlag++ // digital
        }
        if (this.containSpecialChar(password)) {
          securityLevelFlag++ // specialcase
        }
        return securityLevelFlag
      }
    },
    containSpecialChar(str) {
      const containSpecial = RegExp(/[(\ )(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\-)(\_)(\+)(\=)(\[)(\])(\{)(\})(\|)(\\)(\;)(\:)(\')(\")(\,)(\.)(\/)(\<)(\>)(\?)(\)]+/)
      return (containSpecial.test(str))
    },
    goBack() {
      window.history.go(-1)
    }
    // changePass1(value) {
    //   this.visible1 = !(value === "show");
    // },
    // changePass2(value) {
    //   this.visible2 = !(value === "show");
    // },
    // changePass3(value) {
    //   this.visible3 = !(value === "show");
    // },
  }
}
</script>
<style lang="scss" scoped>
.dialog-footer {
  text-align: center;
  margin-top: 40px;
}
.pwd_level {
  overflow: hidden;
  .level_label {
    float: left;
  }
  .level_box {
    float: left;
    margin-left: 4px;
    .level_item {
      display: inline-block;
      width: 30px;
      height: 8px;
      margin-right: 2px;
      background-color: #ddd;
    }
    .level_item_1 {
      background-color: rgb(255, 8, 0);
    }
    .level_item_2 {
      background-color: rgb(255, 157, 0);
    }
    .level_item_3 {
      background-color: rgb(229, 255, 0);
    }
    .level_item_4 {
      background-color: rgb(60, 255, 0);
    }
  }
}

$bg: #31414f;
#container {
  width: 100%;
  //   height: 100%;
  position: relative;
  overflow: hidden;
  background-color: $bg;
}
.navbar {
  height: 60px;
  overflow: hidden;
  position: relative;
  background-color: #061528;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}
.info {
  margin: 100px 120px;
  position: relative;
  background: #fff;
  border-radius: 7px;
  padding: 15px 0px 0px;
  .info-img {
    text-align: center;
    margin-top: -86px;
  }
  .info-title {
    margin: 20px 40px;
    padding-left: 15px;
    height: 40px;
    line-height: 40px;
    background-color: #f2f2f2;
  }
  .info-form {
    width: 75%;
    padding: 30px 0;
    padding-left: 15%;
  }
  .info-bottom {
    background-color: #f5f5f5;
    text-align: center;
    padding: 15px;
    .info-btn {
      width: 120px;
      margin-right: 20px;
    }
  }
}
</style>

